/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DepositBayarPanel.java
 *
 * Created on Nov 4, 2014, 3:06:45 PM
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.DepositDAO;
import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.impl.DepositDAOImpl;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.model.Deposit;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Pembayaran;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author IW20149X
 */
public class DepositBayarPanel extends javax.swing.JPanel {

    Kamar kamar;
    List<GLAccount> listKreditAccount;
    JDialog jDialog;

    public DepositBayarPanel(JDialog jDialog) {
        initComponents();
        renderKreditur();
        this.jDialog = jDialog;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        textKamar = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        textJumlah = new javax.swing.JTextField();
        buttonCari = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        comboKreditur = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Deposit");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Kamar :");

        textKamar.setEditable(false);

        jLabel3.setText("Jumlah :");

        textJumlah.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textJumlahKeyReleased(evt);
            }
        });

        buttonCari.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        buttonCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCariActionPerformed(evt);
            }
        });

        jLabel4.setText("Kreditur :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(textJumlah, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(textKamar, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCari))
                    .addComponent(jLabel4)
                    .addComponent(comboKreditur, 0, 321, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(textKamar)
                    .addComponent(buttonCari, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textJumlah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboKreditur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void textJumlahKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textJumlahKeyReleased
        try {
            Long.parseLong(textJumlah.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Format salah, harus angka", "warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_textJumlahKeyReleased

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        Deposit deposit = new Deposit();
        deposit.setKamar(kamar);
        deposit.setJumlah(textJumlah.getText().isEmpty() ? (double) 0 : new Double(textJumlah.getText()));
        deposit.setStatus("0");
        Pembayaran pembayaran = new Pembayaran();
        pembayaran.setKreditBankAccount(listKreditAccount.get(comboKreditur.getSelectedIndex()));
        pembayaran.setJumlah(deposit.getJumlah());
        validate(deposit, pembayaran);
    }//GEN-LAST:event_buttonSimpanActionPerformed

    private void buttonCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCariActionPerformed
        JDialog jDialogKos = new JDialog();
        jDialogKos.setModal(true);
        jDialogKos.setSize(800, 600);
        KamarPanel kamarPanel = new KamarPanel(jDialogKos, "deposit");
        jDialogKos.add(kamarPanel);
        jDialogKos.setLocationRelativeTo(null);
        jDialogKos.setVisible(true);
        this.kamar = kamarPanel.getKamarFasilitas();
        textKamar.setText(kamarPanel.getKamarFasilitas() == null ? null : kamarPanel.getKamarFasilitas().getKodeKamar());
    }//GEN-LAST:event_buttonCariActionPerformed

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        jDialog.dispose();
    }//GEN-LAST:event_buttonBatalActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonCari;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JComboBox comboKreditur;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField textJumlah;
    private javax.swing.JTextField textKamar;
    // End of variables declaration//GEN-END:variables

    private void validate(Deposit deposit, Pembayaran pembayaran) {
        try {
            if (deposit.getJumlah() <= 0) {
                JOptionPane.showMessageDialog(this, "Jumlah tidak boleh dibawah atau sama dengan 0");
            } else {
                DepositDAO depositDAO = new DepositDAOImpl();
                depositDAO.saveDeposit(deposit, pembayaran);
                textKamar.setText("");
                textJumlah.setText("");
            }
        } catch (Exception ex) {
            Logger.getLogger(DepositBayarPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void renderKreditur() {
        try {
            GLAccountDAO gLAccountDAO = new GLAccountDAOImpl();
            listKreditAccount = gLAccountDAO.getDataByEquals(GLAccount.class, "groupACC", "Kreditur");
            updateComboKreditur();
        } catch (Exception ex) {
            Logger.getLogger(DepositBayarPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateComboKreditur() {
        Object data[] = new Object[listKreditAccount.size()];
        int x = 0;
        for (GLAccount gLAccount : listKreditAccount) {
            data[x] = gLAccount.getNameGL() + " A/C No " + gLAccount.getNoGL();
            x++;
        }
        comboKreditur.setModel(new DefaultComboBoxModel(data));
    }
}
